#include "server_hua.h"
#include "sql_mr.h"
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <pwd.h>
#include <shadow.h>
struct passwdsend
{
        char pw_name[100];               // Username.
        char pw_passwd[2];              //Password.
        __uid_t pw_uid;              // User ID.
        __gid_t pw_gid;              // Group ID.
        char pw_gecos[100];              // Real name.
        char pw_dir[100];                // Home directory.
        char pw_shell[100];              // Shell program.
};
struct spwdsend
{
    char sp_namp[100];         // Login name
        char sp_pwdp[100];         // Encrypted password
        long sp_lstchg;        // Date of last change
        long sp_min;          //  Min #days between changes
        long sp_max;          //  Max #days between changes
        long sp_warn;         //  #days before pwd expires to warn user to change it
        long sp_inact;        //  #days after pwd expires until account is disabled
        long sp_expire;        // #days since 1970-01-01 until account is disabled
        unsigned long sp_flag; // Reserved
};

//发送spwd结构体
void sendspwd(struct spwdsend p,char *status_hua,int ConnectFD)
{
        char temp[1000];
        memset(temp,0x00,sizeof(temp));
        char *pt=temp;
        unsigned char nn;
        nn=strlen(p.sp_namp)+1;
        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,p.sp_namp,(int)nn);
        pt+=int(nn);

        nn=strlen(p.sp_pwdp)+1;
        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,p.sp_pwdp,nn);
        pt+=(int)nn;


        nn=sizeof(long);
        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_lstchg,sizeof(long));
        pt+=sizeof(long);


        memcpy(pt,&nn,sizeof(long));
        pt++;
        memcpy(pt,&p.sp_min,sizeof(long));
        pt+=sizeof(long);

        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_max,sizeof(long));
        pt+=sizeof(long);

        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_warn,sizeof(long));
        pt+=sizeof(long);

        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_inact,sizeof(long));
        pt+=sizeof(long);

        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_expire,sizeof(long));
        pt+=sizeof(long);
        nn=sizeof(unsigned long);
        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,&p.sp_flag,(int)nn);
        pt+=sizeof(unsigned long);

        nn=strlen(status_hua)+1;
        memcpy(pt,&nn,1);
        pt++;
        memcpy(pt,status_hua,(int)nn);
        pt+=(int)nn;
        cout<<"发送到spwd结构体数据大小为："<<send(ConnectFD,temp,pt-temp+1,0)<<endl;
        //cout<<(long)(pt-temp)<<endl;
//	cout<<"向客户端发送的密码是："<<p.sp_pwdp<<endl;
//	memset(&p,0x00,sizeof(struct spwdsend));
	/*cout<<"-----------------------------------------"<<endl;
	cout<<p.sp_pwdp<<" "<<endl;
	cout<<p.sp_namp<<endl;
	cout<<p.sp_min<<endl;
	cout<<p.sp_max<<endl;
	cout<<p.sp_warn<<endl;
	cout<<p.sp_inact<<endl;
	cout<<p.sp_expire<<endl;
	cout<<p.sp_flag<<endl;
	cout<<"用来判断用户状态"<<status_hua<<endl;*/
	//char status_back[500];
	//recv(ConnectFD,status_back,sizeof(status_back),0);
       // cout<<atol(temp)<<endl;
	//cout<<"收到的数据是";
	//for(int ll=0;ll<300;ll++)
	//{
		//cout<<status_back[ll];
	//}cout<<endl;
	//cout<<"原始发送的数据是";
	//for(int ll=0;ll<sizeof(temp);ll++)
	//	cout<<temp[ll];cout<<endl;
        //cout<<"status back"<<status_back<<endl;

	//cout<<"-----------------------------------------"<<endl;
	 memset(&p,0x00,sizeof(struct spwdsend));
	memset(temp,0x00,sizeof(temp));




}
/*
*将发送到信息合并为单次发送，这样就不需要客户端等待
*/
void senddata(struct passwdsend p,char *status_hua,int ConnectFD)
{
      char temp[1000];
        memset(temp,0x00,sizeof(temp));
        char *pt=temp;
        unsigned char nn;
        nn=strlen(p.pw_name)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,p.pw_name,strlen(p.pw_name));
        pt+=strlen(p.pw_name)+1;
        nn=strlen(p.pw_passwd)+1;
        memcpy(pt,&nn,sizeof(nn));
        cout<<nn<<endl;
        pt+=sizeof(nn);
        memcpy(pt,p.pw_passwd,strlen(p.pw_passwd));
        pt+=strlen(p.pw_passwd)+1;
         nn=sizeof(p.pw_uid)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,&p.pw_uid,sizeof(p.pw_uid));
        pt+=sizeof(p.pw_uid)+1;
        nn=sizeof(p.pw_gid)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,&p.pw_gid,sizeof(p.pw_gid));
        pt+=sizeof(p.pw_gid)+1;
        nn=strlen(p.pw_gecos)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,p.pw_gecos,strlen(p.pw_gecos));
        pt+=strlen(p.pw_gecos)+1;
        nn=strlen(p.pw_dir)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,p.pw_dir,strlen(p.pw_dir));
        pt+=strlen(p.pw_dir)+1;
        nn=strlen(p.pw_shell)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,p.pw_shell,strlen(p.pw_shell));
        pt+=strlen(p.pw_shell)+1;

       // char status_hua[2]="o";
        nn=strlen(status_hua)+1;
        memcpy(pt,&nn,sizeof(nn));
        pt+=sizeof(nn);
        memcpy(pt,status_hua,strlen(status_hua)+1);
        pt+=strlen(status_hua)+1;
        cout<<"发送到数据大小为："<<send(ConnectFD,temp,pt-temp+1,0)<<endl;
       // cout<<"向客户端发送的UID是："<<p.pw_uid<<endl;
        memset(&p,0x00,sizeof(struct passwdsend));
        memset(temp,0x00,sizeof(temp));
//	cout<<"已经清空 向客户端发送的UID："<<p.pw_uid<<endl;

}
